package com.sinoglobal.wallet.adapter;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * All adapters basic class
 * @author KEZHUANG
 * @since 2013-5-1
 */
public abstract class W_SinoBaseSimpleAdapter<T> extends BaseAdapter {
	private LayoutInflater mInflater;
	
	/** context对象*/
	protected Context mContext;
	
	/** Adapter数据存放*/
	protected List<T> mDatas;

	/**
	 *  查找 控件
	 * @param widgets 把控件存放到这个map集合中
	 * @param position ListView当前位置
	 */
	public abstract void findViews(Map<String, View> widgets,int position,View convertView);

	/**
	 * 设置控件的值
	 * @param widgets 从这个map集合中取出控件
	 */
	public abstract void setValues(View convertView,ViewHolder holder,Map<String, View> widgets,int position);

	/**
	 * 设置adapter的view
	 * @param inflater 用这个inflater加载布局
	 * @return
	 */
	public abstract View setAdapterLayout(LayoutInflater inflater,int position);

	public W_SinoBaseSimpleAdapter(Context mContext) {
		super();
		mDatas = new ArrayList<T>();
		this.mContext = mContext;
		mInflater = LayoutInflater.from(mContext);
	}
	
	/**
	 * 设置Apdater的数据
	 * @param data
	 */
	public void setListData(List<T> data){
		this.mDatas = data;
		notifyDataSetChanged();
	}
	
	/**
	 * 清除数据
	 */
	public void clearData(){
		this.mDatas.clear();
		notifyDataSetChanged();
	}

	 public boolean hasData(){
		 return mDatas!=null&&mDatas.size()>0;
	 }

	/**
	 * 移除某条数据
	 * @param position 需要移除的索引
	 */
	public void removePosition(int position){
		T t = mDatas.get(position);
		if(t==null){
			return ;
		}
		mDatas.remove(t);
		notifyDataSetChanged();
	}
	
	/**
	 * @return  获得数据
	 */
	public List<T> getListData(){
		return mDatas;
	}
	
	@Override
	public int getCount() {
		if (mDatas != null && mDatas.size() > 0) {
			return mDatas.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if (mDatas != null && mDatas.size() > 0) {
			return mDatas.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return getView(position, convertView);
	}

	@SuppressWarnings("unchecked")
	protected View getView(int position, View convertView) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = setAdapterLayout(mInflater,position);
			viewHolder.findView(position,convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.position = position;
		viewHolder.setValue(convertView,viewHolder,position);
		return convertView;
	}

	protected class ViewHolder {
		public int position;
		public Map<String, View> widgets = new HashMap<String, View>();

		private void findView(int position,View convertView) {
			findViews(widgets,position,convertView);
		}

		private void setValue(View convertView,ViewHolder holder,int position) {
			setValues(convertView,holder,widgets,position);
		}
	}
	
	/**
	 * 直接放入加载的新数据即可,会自动追加到老数据中
	 * @param moreData 新数据
	 */
	public void setMoreData(List<T> moreData){
		if(moreData==null){
			return ;
		}
		mDatas.addAll(moreData);
		notifyDataSetChanged();
	}
}
